<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['complainservice'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['complainservice'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  // Date handling : Start
  $msg ="";
  $party       = NULL;
  $partySelect = NULL;
  $deliveryChalanArray = array();
  $deliveryChalanCount = 0;
  if(isset($_REQUEST['toDateYear']))
  {
	  $toDate    = $_REQUEST['toDateYear']."-".$_REQUEST['toDateMonth']."-".$_REQUEST['toDateDay']." 23:59:59";
  }
  else
  {
	 // $toDate    = date("Y-m-d");
	}
	if(isset($_REQUEST['fromDateYear']))
  {
	  $fromDate    = $_REQUEST['fromDateYear']."-".$_REQUEST['fromDateMonth']."-".$_REQUEST['fromDateDay']." 00:00:00";
  }
  else
  {
	  $fromDate    = date("Y-m-d");
	}
	
	// Date handling : Start
	// party & supplior selected.
	if($_REQUEST['Challan'] == 'All')
  {
  	$selectDeliveryChalan = "SELECT deliverychallanmaster.deliveryChallanMasterId,deliverychallanmaster.address,deliverychallanmaster.challanType,
                                    deliverychallanmaster.srNo,user.userId,user.userName,DATE_FORMAT(deliverychallanmaster.delivaryDate,'%d-%m-%Y') AS delivaryDate,
                                    supplier.supplierId,supplier.companyName
				  						         FROM deliverychallanmaster
					 						         JOIN user ON user.userId = deliverychallanmaster.userId
						 					         JOIN supplier ON supplier.supplierId = deliverychallanmaster.supplierId
						 					        UNION 
						 					       SELECT deliverychallanmaster.deliveryChallanMasterId,deliverychallanmaster.address,deliverychallanmaster.challanType,
                                    deliverychallanmaster.srNo,user.userId,user.userName,DATE_FORMAT(deliverychallanmaster.delivaryDate,'%d-%m-%Y') AS delivaryDate,
                                    party.partyId,party.companyName
				  						         FROM deliverychallanmaster
					 						         JOIN user ON user.userId = deliverychallanmaster.userId
						 					         JOIN party ON party.partyId = deliverychallanmaster.partyId";
	  
  }
  else
  {
    if($_REQUEST['Challan'] == 'Delivery Challan/Note' || $_REQUEST['Challan'] == 'Goods Rejection')
    {
	    $party = "JOIN party ON party.partyId = deliverychallanmaster.partyId";
	    $partySelect = "party.partyId,party.companyName";
    }
    else
    {
	    $party = "JOIN supplier ON supplier.supplierId = deliverychallanmaster.supplierId";
	    $partySelect = "supplier.supplierId,supplier.companyName";
	  }
      $selectDeliveryChalan = "SELECT deliverychallanmaster.deliveryChallanMasterId,deliverychallanmaster.address,deliverychallanmaster.challanType,
                                  deliverychallanmaster.srNo,user.userId,user.userName,DATE_FORMAT(deliverychallanmaster.delivaryDate,'%d-%m-%Y') AS delivaryDate,
                                  ".$partySelect."
				  						       FROM deliverychallanmaster
					 						       JOIN user ON user.userId = deliverychallanmaster.userId
						 					       ".$party."";
    if(isset($_REQUEST['companyId']))
    {
      if($_REQUEST['companyId'] > 0 )
      {
      	if($_REQUEST['Challan'] == 'Delivery Challan/Note' || $_REQUEST['Challan'] == 'Goods Rejection')
      	{
          $selectDeliveryChalan .= " AND deliverychallanmaster.partyId = ".$_REQUEST['companyId'];
        }
        else
        {
        	$selectDeliveryChalan .= " AND deliverychallanmaster.supplierId = ".$_REQUEST['companyId'];
        }
      }
    }
    if(isset($_REQUEST['Challan']) && $_REQUEST['Challan'] != '')
    {
      if($_REQUEST['Challan'] != '0')
	    {
	  	  $selectDeliveryChalan .= " AND deliverychallanmaster.challanType = '".$_REQUEST['Challan']."' ";
	    }
    }
    if(isset($_REQUEST['toDateMonth'] ) && $_REQUEST['toDateMonth'] != '')
    {
      $selectDeliveryChalan .= " AND deliverychallanmaster.delivaryDate >= '".$fromDate."' 
                                 AND deliverychallanmaster.delivaryDate <= '".$toDate."' 
                               ORDER BY deliverychallanmaster.delivaryDate";
    }
  }
  $selectDeliveryResult = mysql_query($selectDeliveryChalan);
	while($deliveryChalanRow = mysql_fetch_array($selectDeliveryResult))
	{
	  $deliveryChalanArray[$deliveryChalanCount]['deliveryChallanMasterId'] = $deliveryChalanRow['deliveryChallanMasterId'];
	  
	  $deliveryChalanArray[$deliveryChalanCount]['companyName']             = $deliveryChalanRow['companyName'];
	  $deliveryChalanArray[$deliveryChalanCount]['srNo']                    = $deliveryChalanRow['srNo'];
	  $deliveryChalanArray[$deliveryChalanCount]['delivaryDate']            = $deliveryChalanRow['delivaryDate'];
	  $deliveryChalanArray[$deliveryChalanCount]['challanType']             = $deliveryChalanRow['challanType'];
	  $deliveryChalanCount++;
	}  
  if($deliveryChalanCount == 0)
  {
	  $msg = 'Record Not Found...!';
  }
  include("./bottom.php");
  $smarty->assign("msg",$msg);
  $smarty->assign("deliveryChalanArray",$deliveryChalanArray);
  $smarty->display("deliveryChalanAj.tpl");
}
?>